// pages/upload/upload.js
const app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    number:1,
    text:'',
    token:'',
    user_name:'',
    user_image:''
  },
  inputText(e){
    this.setData({text:e.detail.value})
  },
  // 预览图片
  clickimage:function(e){
    var index = e.target.dataset.index
    wx.previewImage({
      urls:[this.data.FilePath[index]]
    })
  },
  addImage:function(e){
    var that = this;
    wx.chooseImage({
      count: 3,
      success:function(res){
        that.setData({
          FilePath:res.tempFilePaths,
          number:res.tempFilePaths.length+1
        })
      }
    })
  },
  deleteImage:function(e){
    var that = this
    var index = e.target.dataset.index
    var tempFilePaths = that.data.FilePath
    wx.showModal({
      title:'提示',
      content:'确定要删除此图片吗？',
      success:function(res){
        if(res.confirm){
          console.log("点击了确定");
          tempFilePaths.splice(index,1)
        }else if(res.cancel){
          console.log("点击取消")
          return false;
        }
        that.setData({
          FilePath:tempFilePaths,
          number:that.data.number-1
        })
        console.log(that.data.FilePath)
      }
    })
  },
  upload:function(){
    var that = this
    // wx.showLoading({
    //   title: '上传中',
    // })
    that.img_upload()
  },

  img_upload:function(f){
    let that = this;
    var token=wx.getStorageSync('token');
    console.log(that.data.text);
    var tempFilePaths = that.data.FilePath;
    var img_url = [];
    if(tempFilePaths==undefined){
        console.log('ok');    
        let textContent=that.data.text;
        console.log(textContent);
        wx.request({
            url:`${app.globalData.url}/user/upload`,
                    data:{
                       text:textContent,
                       img_url
                    },
                    header: {
                      'authorization': token
                    },
                    method:'POST',
                    success:function(res){
                        wx.hideLoading()
                        wx.showModal({
                          title:'正在提交，请稍后',
                          showCancel:false,
                          success:function(res){
                            wx.showModal({
                              title:'提交成功,请在广场页刷新查看',
                              showCancel:false,
                              success:function(res){
                                wx.switchTab({
                                  url: '/pages/daywrite/daywrite'
                                })
                              }
                            })
                          }
                        })
                    }
        })
    }else{
        for(let i=0;i<tempFilePaths.length;i++){
            wx.uploadFile({
              url: `${app.globalData.url}/user/image`,
              filePath: tempFilePaths[i],
              name: 'image',
              header: {
                'authorization': token
              },
              success:function(res){
                console.log("ok");
                let text=that.data.text;
                console.log(text);
                img_url.push(res.data);
                if(img_url.length == tempFilePaths.length){
                  img_url=img_url.toString();
                  console.log(img_url);
                  wx.request({
                    url:`${app.globalData.url}/user/upload`,
                    data:{
                      img_url,
                      text
                    },
                    header: {
                      'authorization': token
                    },
                    method:'POST',
                    success:function(res){
                        wx.hideLoading()
                        wx.showModal({
                          title:'正在提交，请稍后',
                          showCancel:false,
                          success:function(res){
                            wx.showModal({
                              title:'提交成功,请在广场页刷新查看',
                              showCancel:false,
                              success:function(res){
                                wx.switchTab({
                                  url: '/pages/daywrite/daywrite'
                                })
                              }
                            })
                          }
                        })
                    }
                  })
                }
              },
              fail:function(err){
                console.log('上传失败')
                console.log(err)
              }
            })
          }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})